<?php

/**
 * 优惠信息
 * @author Ice <iceinto@mallog.com.cn>
 */
class LimitedAction extends LoginAction
{

    protected $_type = 'limited';

    /**
     * 优惠列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function index()
    {
        $data = D('Limited')->getIndexData();
        $this->assign('frees', $data['data']);
        $this->assign('page', $data['page']);
        $this->display();
    }

    /**
     * 添加优惠
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function add()
    {
        $this->getLoadData();
        if (isPost()) {
            $data = $this->getPostDate();
            $this->createAdd('Limited', $data);
        }
        $this->display();
    }

    /**
     * 修改优惠
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function edit()
    {
        $id = intval(getGet('id'));
        if (!$id) {
            $this->error('信息获取必须提供数据标识！');
        }
        $this->getLoadData();
        //加载优惠信息
        $free = M('Limited')->find($id);
        if (isPost()) {
            $data = $this->getPostDate(true);
            $data['id'] = $id;
            $rs = $this->createUpdate('Limited', $data, false);
            if ($rs) {
                $this->successMessage('修改成功！');
            }
            if ($data['image'] && $rs) {
                if ($data['image'] != $free['image']) {
                    $this->delUpLoadFile($free['image'], $this->_type);
                }
            } else if (!$rs && $data['image']) {
                $this->delUpLoadFile($data['image'], $this->_type);
                $this->error('更新失败！');
            }
            $free = M('Limited')->find($id);
        }
        if ($free) {
            $this->assign('free', $free);
            //获取图片
            $imageUrl = $this->getManagedUrl($free['image'], $this->_type);
            $this->assign('imageUrl', $imageUrl);
        } else {
            $this->error('优惠信息加载失败！');
        }
        $this->display();
    }

    /**
     * 获取提交数据
     * @param bool $isEdit 是否是修改
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getPostDate($isEdit = false)
    {
        $data = array();
        $data['title'] = getPost('title');
        $data['content'] = getPost('content');
        $data['count'] = intval(getPost('count'));
        $data['start_time'] = strtotime(getPost('start_time'));
        $data['end_time'] = strtotime(getPost('end_time'));
        $data['receive_start_time'] = strtotime(getPost('receive_start_time'));
        $data['receive_end_time'] = strtotime(getPost('receive_end_time'));
        $data['mid'] = intval(getPost('market'));
        $data['fid'] = intval(getPost('floor'));
        $data['bid'] = intval(getPost('brand'));
        $data['day_max'] = intval(getPost('day_max')); //每天最多发放
        $data['user_max'] = intval(getPost('user_max')); //用户最多领取
        $data['every_use_count'] = intval(getPost('every_use_count')); //每次消耗好友数量
        $data['user_max'] = $data['use_max'] ? $data['user_max'] : 1;
        $data['model'] = intval(getPost('model'));
        $data['model'] = $data['model'] ? $data['model'] : 1; //默认模式
        if ($data['day_max'] > $data['count']) {
            $this->error('一天的发送数量不能大于总数量！');
        }
        if (!$data['mid']) {
            $this->error('发放商场必须选择！');
        }
        if (!$data['count']) {
            $this->error('请填写正确的数量信息！');
        }
        //优惠活动时间
        if ($data['start_time'] > $data['end_time']) {
            $this->error('优惠活动结束时间不能小于开始时间！');
        }
        //优惠领取时间
        if ($data['receive_start_time'] > $data['receive_end_time']) {
            $this->error('优惠领取结束时间不能小于开始时间！');
        }
        //优惠发放时间不能大于开始领取时间
        if ($data['receive_start_time'] < $data['start_time']) {
            $this->error('优惠领取时间不小于优惠发放时间！');
        }
        if ($isEdit) {
            $count = 0;
            //判断当前数量不能小于已经领取的数量
            $id = intval(getGet('id'));
            $count = D('LimitedUsers')->getCount($id);
            if ($count > $data['count']) {
                $this->error('现有数量要满足领取需求！');
            }
        }
        //文件执行上传并且保存返回媒体库标示
        $doUpload = true;
        if ($isEdit) {
            if (!$_FILES['image']['name']) {
                $doUpload = false;
            }
        }
        if ($doUpload) {
            $fId = $this->uploadFiles($this->_type);
            $data['image'] = $fId; //媒体库标示
            if (!intval($fId)) {
                $this->error('图片删除错误！');
            }
        }
        return $data;
    }

    public function banner()
    {
        $limited = array();
        $LimitedModel = M('Limited');
        $id = intval(getGet('id'));
        if (!$id) {
            $this->error('信息获取必须提供数据标识！');
        }
        //加载优惠信息
        $limited = $LimitedModel->find($id);
        if ($limited) {
            if (isPost()) {
                //条件判断
                $data = array();
                $fId = $this->uploadFiles($this->_type);
                $data['banner_image'] = $fId; //媒体库标示
                $data['id'] = $id;
                //修改信息
                $result = $LimitedModel->save($data);
                if ($result) {
                    $this->delUpLoadFile($limited['banner_image'], $this->_type);
                    $limited = $LimitedModel->find($id);
                    $this->successMessage('横幅修改成功！');
                } else {
                    $this->error('横幅修改失败！');
                }
                //调整后重新加载
                $limited = $LimitedModel->find($id);
            }
            //获取横幅图片
            $bannerImage = $limited['banner_image'];
            if ($bannerImage) {
                $bannerImageUrl = null;
                $bannerImageUrl = $this->getManagedUrl($bannerImage, $this->_type);
                $this->assign('bannerImageUrl', $bannerImageUrl);
            }
            $this->assign('limited', $limited);
        } else {
            $this->error('优惠信息错误！');
        }
        $this->display();
    }

    /**
     * 加载需要加载的数据
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getLoadData()
    {
        $market = M('Market')->field('id,name')->select();
        if (!$market) {
            $this->error('商场列表不能为空！');
        }
        $this->assign('market', $market);
    }

}

?>
